Materials+ML Workshop Day 2¶
Day 2 Agenda:¶
- Questions about Day 1 Material
- Review of Day 1
- Python Data Types:
- Lists
- Tuples
- Sets
- Dictionaries
- Python Functions
- Python Classes
- Object-Oriented Programming
The Workshop Online Book:¶
https://cburdine.github.io/materials-ml-workshop/¶
Tentative Week 1 Schedule:¶
Session | Date | Content |
Day 1 | 06/09/2025 (2:00-4:00 PM) | Introduction, Python Data Types |
Day 2 | 06/10/2025 (2:00-4:00 PM) | Python Functions and Classes |
Day 3 | 06/11/2025 (2:00-4:00 PM) | Scientific Computing with Numpy and Scipy |
Day 4 | 06/12/2025 (2:00-4:00 PM) | Data Manipulation and Visualization |
Day 5 | 06/13/2025 (2:00-4:00 PM) | Materials Science Packages, Introduction to ML |
Install Workshop Requirements:¶
Make sure you have installed the Python packages needed for this workshop:
Copy the command from cburdine.github.io/materials-ml-workshop/ in the Getting Started section.
pip install -r https://gist.github.com/cburdine/.../requirements.txt
Questions¶
Material covered yesterday:
- Python Types
- Variables
- Boolean variables
- Conditional statements and comparison operators
- Loops (
for
andwhile
)
Review: Day 1¶
Variables¶
In [1]:
pi_approximation = 3.14 # float variable
number_of_steps = 100 # int variable
name = 'James T. Kirk' # str variable
Conditional Statements¶
In [2]:
numerator = 10.0
denominator = 2.0
quotient = 0.0
# perform division only if the denominator is nonzero:
if denominator != 0:
quotient = numerator / denominator
print(quotient)
5.0
if
/elif
/else
Statements¶
In [3]:
# initialize a numeric value:
value = 100.0
# print out the sign of the value:
if value < 0:
print('Value is negative.')
elif value == 0:
print('Value is zero.')
else:
print('Value is positive.')
Value is positive.
While loop¶
In [4]:
step = 1
# execute the `while` loop:
while step < 4:
print(step)
step += 1
# print "Done" upon completion of the loop:
print('Done')
1 2 3 Done
For loop¶
In [5]:
for step in range(1,4):
print(step)
print('Done')
1 2 3 Done
- We can also use
for
loops with sequences of items:
In [6]:
for fruit in ['Orange', 'Kiwi', 'Mango']:
print(fruit)
Orange Kiwi Mango
- The data enclosed in
[
...]
is called a list.
Tutorial: Python Data Types¶
- Lists and tuples store sequences of items:
In [7]:
# example of a list
my_list = [ 1, 2, 3, 4 ]
In [8]:
# example of a tuple
my_tuple = ( 1, 2, 3, 4 )
- Lists are mutable (they can be modified)
- Tuples are immutable (they cannot be modified)
Exercises: Python Data Types¶
- Sieve of Eratosthenes
Tutorial: Sets and Dictionaries¶
- Sets and dictionaries keep track of unique items:
In [9]:
# example of a set
my_set = { 1, 2, 3, 4 }
In [10]:
# example of a dictionary
my_dict = { 'A': 1.0, 'B': 2.0 }
- Sets store an unordered collection of unique objects
- Dictionaries store key-value pairs
Exercises: Sets and Dictionaries¶
- Scheduling
- Most Frequent Number
Tutorial: Python Functions and Classes¶
In [11]:
# example of a function
def my_function(x, y):
z = (x**2 + y**2)**0.5
return z
- Python functions make your code re-usable and modular:
In [12]:
print(my_function(3, 4))
5.0
Exercises: Python Functions and Classes¶
- List Statistics
- Star Rectangle
Tutorial: Classes and Object-Oriented Programming¶
- Classes serve as a "blueprint" for user-defined Python objects
In [13]:
class MyPythonClass:
""" This is an example of a Python Class """
def __init__(self, x_value):
""" This is a constructor """
self.x = x_value
In [14]:
instance = MyPythonClass(10)
print(instance.x)
10
Exercise: Classes and Object-Oriented Programming¶
- Inorganic Material Class
Review¶
Lists and Tuples¶
- Lists and tuples are ordered sequences of data
- Lists are mutable (they can be modified)
- Tuples are immutable (they cannot be modified)
In [15]:
my_list = [ 'A', 'B', 'C', 'D' ]
my_tuple = ('A', 'B', 'C', 'D' )
Sets and Dictionaries¶
- Sets store an unordered collection of unique objects
- Dictionaries store key-value pairs
In [16]:
my_set = { 'A', 'B', 'C', 'D', 'C'}
my_dict = { 'A' : 1, 'B' : 11, 'C' : 12, 'D' : 0}
print(my_set)
print(my_dict)
{'B', 'D', 'C', 'A'} {'A': 1, 'B': 11, 'C': 12, 'D': 0}
Python Functions¶
- We have already encountered few functions in Python, such as
print()
- We have also seen functions associated with lists and tuples:
In [17]:
# len() returns the length of a tuple, list, set, str, or dict:
my_list = [ 1, 2, 3, 4 ]
print( len(my_list) )
4
In [18]:
# list.append() adds a value to the end of a list
my_list.append(10)
print(my_list)
[1, 2, 3, 4, 10]
More examples of functions:¶
In [19]:
# set.add() adds a value to a set:
my_set = {1,2,3}
my_set.add(2)
my_set.add(5)
print(my_set)
{1, 2, 3, 5}
In [20]:
my_dict = { 'A' : 0, 'B' : 2, 'C' : 1 }
# dict.values() returns the values in a dictionary:
dict_vals = list( my_dict.values() )
print(dict_vals)
[0, 2, 1]
Writing Python Functions¶
In [21]:
# create a function to print out a greeting:
def greet():
print('Hello there!')
- The name of the function (
greet
) follows thedef
keyword - After the function name is list parameters in parentheses (there none in this case)
- The function is followed by an indented block of code (the function body)
- A function can optionally output a value using the
return
statement.
Function parameters and returned values:¶
In [22]:
# create a function to add two numbers:
def add_numbers(a,b):
total = a + b
return total # <-- output of function
# store the returned value of a + b in `result`:
result = add_numbers(3,5)
print(result)
8
- The parameters (
a
andb
) are "temporary variables that store the values that are passed to function. - Returned values can be assigned to a variable when the function is called
Documenting Functions¶
- It is important to write comments that describe what a function does.
- We do this with special comments called docstrings (
"""
..."""
)
In [23]:
def greet(name = 'Albert'):
""" prints a greeting for the given name """
print('Hello, ' + name + '!')
- Function docstrings get printed when the
help
function is called:
In [24]:
help(greet)
Help on function greet in module __main__: greet(name='Albert') prints a greeting for the given name
Objects in Python¶
- In computer programming, an object is something that contains data and has prescribed behaviors.
- In Python, every data type that we have encountered so far (
list
,str
,set
, etc.) is an object.
- The data and behaviors of an object are described in something called a class
- Classes are an important concept in Object-Oriented Programming (OOP)
Python Classes¶
- Classes serve as a kind of blueprint for objects.
- We can define our own classes in Python as follows:
- Classes typically represent real-world objects, concepts, or services.
Constructing Instances of a Class¶
In [25]:
class Dog:
""" This class represents a pet dog """
def __init__(self, dog_name):
""" Constructs a Dog instance with given name """
self.name = dog_name
In [26]:
# construct two different Dog objects:
my_dog = Dog('Snoopy')
my_other_dog = Dog('Fido')
In [27]:
# print the names of the dogs:
print(my_dog.name, my_other_dog.name)
# assign the name of one dog to 'Rover'
my_other_dog.name = 'Rover'
print(my_dog.name, my_other_dog.name)
Snoopy Fido Snoopy Rover
Questions¶
Recommended Online Book Reading:¶
- Scientific Computing with Numpy and Scipy
Bring your questions to our next meeting tomorrow!